package cn.edu.nju.software.cdiy.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.edu.nju.software.cdiy.algorithm.performance.CPUEvaluator;
import cn.edu.nju.software.cdiy.bean.Brand;
import cn.edu.nju.software.cdiy.bean.CPU;
import cn.edu.nju.software.cdiy.bean.Chassis;
import cn.edu.nju.software.cdiy.bean.Hardware;

public class Changer {
	public static List<Hardware> toHardwareList(List l) {
		List<Hardware> list = new ArrayList<Hardware>();
		for (int i = 0; i < l.size(); i++) {
			list.add((Hardware) l.get(i));
		}

		return list;
	}

	public static boolean checkPrice(Hardware h, String s) {
		int price = h.getoPrice();
		int priceArray[] = NumberFormatter.getPriceArray(s);
		if (priceArray[1] == 0) {
			if (s.charAt(s.length() - 1) == '上') {
				if (h.getoPrice() >= priceArray[0])
					return true;
			} else if (s.charAt(s.length() - 1) == '下') {
				if (h.getoPrice() <= priceArray[0])
					return true;
			}
		} else {
			if (h.getoPrice() >= priceArray[0]
					&& h.getoPrice() <= priceArray[1])
				return true;
		}
		return false;
	}

	public static void setUniAttribute(Hardware h, String key, List<String> l) {
		if (key.equals(Constant.commonInforStr[0])) {
			h.setCSPhone(l.get(0));
		} else if (key.equals(Constant.commonInforStr[1])) {
			h.setQualityPolicy(l.get(0));
		} else if (key.equals(Constant.commonInforStr[2])) {
			h.setQualityTime(l.get(0));
		} else if (key.equals(Constant.commonInforStr[3])) {
			h.setQualityContent(l.get(0));
		} else if (key.equals(Constant.commonInforStr[4])) {
			h.setiPrice(Integer.parseInt(l.get(0)));
			h.setoPrice((int) (1.1*Integer.parseInt(l.get(0))));
		} else if (key.equals(Constant.commonInforStr[5])) {
			h.setoPrice(Integer.parseInt(l.get(0)));
		} else if (key.equals(Constant.commonInforStr[6])) {
			Date d = null;
			try {
				d = StringToDate(l.get(0), "yyyy-MM-dd hh:mm:ss");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			h.setInStorageTime(d);
		} else if (key.equals(Constant.commonInforStr[7])) {
			h.setImgPath(l.get(0));
		} else if (key.equals(Constant.commonInforStr[9])) {
//			System.out.println("supplier:"+l.get(0));
			h.setSupplier(Integer.parseInt(l.get(0)));
		}
		if(h.getInStorageTime()==null)	h.setInStorageTime(new Date());

	}

	public static Date StringToDate(String dateStr, String formatStr)
			throws ParseException {
		DateFormat sdf = new SimpleDateFormat(formatStr);
		Date date = null;

		date = sdf.parse(dateStr);

		return date;
	}

	public static void setDefaultAttribute(Hardware h) {
		// 填充网友评分、专家评分、销售量、id
		h.setwScore(Constant.WSCORE_DEFAULT);
		h.setzScore(Constant.ZSCORE_DEFAULT);
		h.setSale(0);
		h.setId(0);

		
	}

	public static String getSqlHeader(HardwareType ht){
		String returnS=null;
		String stateSql = "WHERE STATE=882 ";
		switch(ht){ 
		case CHA:
			returnS="FROM Chassis "+stateSql;
		case CPU:
			returnS="FROM CPU "+stateSql;
		case DISK:
			returnS="FROM Disk "+stateSql;
		case DRI:
			returnS="FROM Driver "+stateSql;
		case KBD:
			returnS="FROM Keyboard "+stateSql;
		case MBD:
			returnS="FROM MainBoard "+stateSql;
		case GRA:
			returnS="FROM Graphics "+stateSql;
		case MEM:
			returnS="FROM Memory "+stateSql;
		case MON:
			returnS="FROM Monitor "+stateSql;
		case MOU:
			returnS="FROM Mouse "+stateSql;
		case NET:
			returnS="FROM NetworkAdapter "+stateSql;
		case POW:
			returnS="FROM Power "+stateSql;
		case RAD:
			returnS="FROM Radiator "+stateSql;
		case SBX:
			returnS="FROM SoundBox "+stateSql;
		case SCD:
			returnS="FROM SoundCard "+stateSql;
		default:
			break;

		}
		return null;
	}
}
